<?php
namespace app\erp\model;

use think\Model;
use traits\model\SoftDelete;
use app\erp\validate\AccessConfigrule;
use think\Cache;

class AccessConfigdb extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $pk = 'access_config_id';

    protected $name = 'access_config';

    protected $type = [
        'create_time' => 'timestamp:Y-m-d',
        'delete_time' => 'timestamp:Y-m-d'
    ];

    protected $auto = [
        'controller_name'
    ];

    protected function setControllerNameAttr($value)
    {
        return ucfirst($value);
    }
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;

    public $param = array();

    protected function cache_name($name)
    {
        $new_arr = $this->param;
        $new_arr['partner_id'] = PARTNER_ID;
        if (! isset($new_arr['site_id'])) {
            $new_arr['site_id'] = SITE_ID;
        }
        ksort($new_arr);
        $param_str = http_build_query($new_arr);
        return md5($name . $param_str);
    }

    public function scopeModule($query, $module)
    {
        $query->where('module', $module);
    }

    public function scopeControllerName($query, $controller_name)
    {
        $query->where('controller_name', 'like', '%' . $controller_name . '%');
    }

    public function scopeFunctionName($query, $function_name)
    {
        $query->where('function_name', 'like', '%' . $function_name . '%');
    }

    public function scopeUseIntro($query, $use_intro)
    {
        $query->where('use_intro', 'like', '%' . $use_intro . '%');
    }

    public function scopeAccessConfigId($query, $access_config_id)
    {
        $query->where('access_config_id', 'IN', $access_config_id);
    }

    public function base($query)
    {
        $field = $this->getDeleteTimeField(true);
        if ($field) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->onlyTrashed();
            } else {
                $query->useSoftDelete($field);
            }
        }
        if (isset($this->param['module']) && $this->param['module']) {
            $query->module($this->param['module']);
        }
        if (isset($this->param['controller_name']) && $this->param['controller_name']) {
            $query->controllerName($this->param['controller_name']);
        }
        if (isset($this->param['function_name']) && $this->param['function_name']) {
            $query->functionName($this->param['function_name']);
        }
        if (isset($this->param['access_config_id']) && $this->param['access_config_id']) {
            $query->accessConfigId($this->param['access_config_id']);
        }
        if (isset($this->param['use_intro']) && $this->param['use_intro']) {
            $query->useIntro($this->param['use_intro']);
        }
    }

    public function ajaxSelect()
    {
        $data['data'] = $this->ajaxList();
        $data['count'] = $this->ajaxCount();
        return $data;
    }

    public function ajaxList()
    {
        $data = $this->page($this->param['page'], $this->param['limit'])
            ->cache($this->cache_name('access_config_list'), 86400, 'accessConfig')
            ->order('module asc,controller_name asc,access_config_id asc')
            ->select();
        if ($data) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function ajaxCount()
    {
        $data = $this->cache($this->cache_name('access_config_count'), 86400, 'accessConfig')
            ->count();
        return $data;
    }

    public function ajaxFind()
    {
        $data = $this->master()->find();
        if ($data) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function ajaxSave()
    {
        $rule = new AccessConfigrule();
        if (! $rule->check($this->param)) {
            return $rule->getError();
        }
        if (isset($this->param['access_config_id']) && $this->param['access_config_id'] != '') {
            $this->save($this->param, [
                'access_config_id' => $this->param['access_config_id']
            ]);
        } else {
            $this->allowField(true)->save($this->param);
        }
        Cache::clear('accessConfig');
        return 'success';
    }
    // 软删除

	
	
    public function ajaxDelete()
    {
        if (isset($this->param['access_config_id'])) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->destroy(function ($query) {
                    $query->where('access_config_id', 'IN', $this->param['access_config_id']);
                }, true);
            } else {
                $this->destroy(function ($query) {
                    $query->where('access_config_id', 'IN', $this->param['access_config_id']);
                }, true);
            }
            Cache::clear('accessConfig');
            return 'success';
        }
    }
}